package org.eclipse.iota.ui.application;

import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

/**
 * An action bar advisor is responsible for creating, adding, and disposing of
 * the actions added to a workbench window. Each window will be populated with
 * new actions.
 */
public class IotaActionBarAdvisor extends ActionBarAdvisor {

	private ActionFactory.IWorkbenchAction lockToolBarAction;
	private ActionFactory.IWorkbenchAction toggleCoolbarAction;

	public IotaActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
	}

	protected IWorkbenchWindow getWindow() {
		return getActionBarConfigurer().getWindowConfigurer().getWindow();
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.application.ActionBarAdvisor#makeActions(org.eclipse.ui.IWorkbenchWindow)
	 */
	@Override
	protected void makeActions(IWorkbenchWindow window) {

		toggleCoolbarAction = ActionFactory.TOGGLE_COOLBAR.create(window);
		register(toggleCoolbarAction);
		lockToolBarAction = ActionFactory.LOCK_TOOL_BAR.create(window);
		register(lockToolBarAction);

		register(ActionFactory.CLOSE.create(window));
		register(ActionFactory.CLOSE_ALL.create(window));
		register(ActionFactory.SAVE.create(window));
		register(ActionFactory.SAVE_AS.create(window));
		register(ActionFactory.SAVE_ALL.create(window));
		register(ActionFactory.QUIT.create(window));
		register(ActionFactory.UNDO.create(window));
		register(ActionFactory.REDO.create(window));
		register(ActionFactory.CUT.create(window));
		register(ActionFactory.COPY.create(window));
		register(ActionFactory.PASTE.create(window));
		register(ActionFactory.DELETE.create(window));
		register(ActionFactory.SELECT_ALL.create(window));
		register(ActionFactory.OPEN_NEW_WINDOW.create(window));
		register(ActionFactory.PRINT.create(window));

		register(ActionFactory.RESET_PERSPECTIVE.create(window));
		register(ActionFactory.PREFERENCES.create(window));
		register(ActionFactory.ABOUT.create(window));

	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.application.ActionBarAdvisor#fillMenuBar(org.eclipse.jface.action.IMenuManager)
	 */
	@Override
	protected void fillMenuBar(IMenuManager menu) {

		IMenuManager fileMenu = new MenuManager("&File", IWorkbenchActionConstants.M_FILE);
		fileMenu.add(new GroupMarker(IWorkbenchActionConstants.FILE_START));

		IMenuManager newMenu = new MenuManager("&New", "new");
		newMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		fileMenu.add(newMenu);

		fileMenu.add(new Separator());
		fileMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		fileMenu.add(new Separator());
		fileMenu.add(getAction(ActionFactory.CLOSE.getId()));
		fileMenu.add(getAction(ActionFactory.CLOSE_ALL.getId()));
		fileMenu.add(new Separator());
		fileMenu.add(getAction(ActionFactory.SAVE.getId()));
		fileMenu.add(getAction(ActionFactory.SAVE_AS.getId()));
		fileMenu.add(getAction(ActionFactory.SAVE_ALL.getId()));
		fileMenu.add(new Separator());
		fileMenu.add(getAction(ActionFactory.QUIT.getId()));
		fileMenu.add(new GroupMarker(IWorkbenchActionConstants.FILE_END));
		menu.add(fileMenu);

		IMenuManager editMenu = new MenuManager("&Edit", IWorkbenchActionConstants.M_EDIT);

		editMenu.add(new GroupMarker(IWorkbenchActionConstants.EDIT_START));
		editMenu.add(getAction(ActionFactory.UNDO.getId()));
		editMenu.add(getAction(ActionFactory.REDO.getId()));
		editMenu.add(new GroupMarker(IWorkbenchActionConstants.UNDO_EXT));
		editMenu.add(new Separator());
		editMenu.add(getAction(ActionFactory.CUT.getId()));
		editMenu.add(getAction(ActionFactory.COPY.getId()));
		editMenu.add(getAction(ActionFactory.PASTE.getId()));
		editMenu.add(new GroupMarker(IWorkbenchActionConstants.CUT_EXT));
		editMenu.add(new Separator());
		editMenu.add(getAction(ActionFactory.DELETE.getId()));
		editMenu.add(getAction(ActionFactory.SELECT_ALL.getId()));
		editMenu.add(new Separator());
		editMenu.add(new GroupMarker(IWorkbenchActionConstants.ADD_EXT));
		editMenu.add(new GroupMarker(IWorkbenchActionConstants.EDIT_END));
		editMenu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
		editMenu.add(getAction(ActionFactory.RESET_PERSPECTIVE.getId()));
		editMenu.add(new Separator());
		editMenu.add(getAction(ActionFactory.PREFERENCES.getId()));

		menu.add(editMenu);
		menu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));

		// Don't add the window menu:
		//IMenuManager windowMenu = new MenuManager(
		//		Messages.ApplicationMenuName_Window,
		//		IWorkbenchActionConstants.M_WINDOW);

		//windowMenu.add(getAction(ActionFactory.OPEN_NEW_WINDOW.getId()));
		//windowMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		//menu.add(windowMenu);

		IMenuManager helpMenu = new MenuManager("&Help", IWorkbenchActionConstants.M_HELP);

		helpMenu.add(new GroupMarker(IWorkbenchActionConstants.HELP_START));
		helpMenu.add(getAction(ActionFactory.ABOUT.getId()));
		helpMenu.add(new GroupMarker(IWorkbenchActionConstants.HELP_END));
		helpMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		menu.add(helpMenu);

	}

	/**
	 * @generated
	 */
	protected void fillCoolBar(ICoolBarManager toolBar) {
		IMenuManager popUpMenu = new MenuManager();
		popUpMenu.add(new ActionContributionItem(lockToolBarAction));
		popUpMenu.add(new ActionContributionItem(toggleCoolbarAction));
		toolBar.setContextMenuManager(popUpMenu);

		toolBar.add(new GroupMarker("group.file"));

		{
			IToolBarManager toolBarX = new ToolBarManager();
			toolBarX.add(new Separator(IWorkbenchActionConstants.NEW_GROUP));
			toolBarX.add(new GroupMarker(IWorkbenchActionConstants.NEW_EXT));
			toolBarX.add(new GroupMarker(IWorkbenchActionConstants.SAVE_GROUP));
			toolBarX.add(getAction(ActionFactory.SAVE.getId()));
			toolBarX.add(new GroupMarker(IWorkbenchActionConstants.SAVE_EXT));
			toolBarX.add(getAction(ActionFactory.PRINT.getId()));
			toolBarX.add(new GroupMarker(IWorkbenchActionConstants.PRINT_EXT));
			toolBarX.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
			toolBar.add(new ToolBarContributionItem(toolBarX,
					IWorkbenchActionConstants.TOOLBAR_FILE));
		}

		toolBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		toolBar.add(new GroupMarker("group.nav"));
		toolBar.add(new GroupMarker(IWorkbenchActionConstants.GROUP_EDITOR));
		toolBar.add(new GroupMarker(IWorkbenchActionConstants.GROUP_HELP));

		{
			IToolBarManager toolBarX = new ToolBarManager();
			toolBarX.add(new Separator(IWorkbenchActionConstants.GROUP_HELP));
			toolBarX.add(new GroupMarker(IWorkbenchActionConstants.GROUP_APP));
			toolBar.add(new ToolBarContributionItem(toolBarX,
					IWorkbenchActionConstants.TOOLBAR_HELP));
		}
	}
}
